package com.zz.man.service;

import com.zz.man.domian.Man;
import com.zz.man.enums.ResultEnum;
import com.zz.man.exception.ManException;
import com.zz.man.repository.ManRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class ManService {

    @Autowired
    private ManRepository manRepository;

    @Transactional
    public void insertTwo(){
        Man mana = new Man();
        mana.setAge(5);
        mana.setName("娜");
        manRepository.save(mana);

        Man manb = new Man();
        manb.setAge(323234);
        manb.setName("李静士大夫撒旦 阿斯蒂芬");
        manRepository.save(manb);
    }

    public void getAge(Integer id)throws Exception{
        Optional<Man> byId = manRepository.findById(id);
        Integer age = byId.get().getAge();
        if (age<10){
            /** 可能在上小学. */
            throw new ManException(ResultEnum.PRIMARY_SCHOOL);
        }else if (age>10 && age<16){
            /** 可能在初中吧. */
            throw new ManException(ResultEnum.MIDDLE_SCHOOL);
        }else {
            /** 可能在。。。. */
//            throw new ManException(101,"可能在大学吧");
        }
    }

    /**
     * 通过id查询一个女生信息
     * @param id
     * @return
     */
    public Optional<Man> findOne(Integer id){
        return manRepository.findById(id);
    }
}